<script setup>
import { nextTick, ref, watch } from "vue";
import { Local } from "../../utils/storage";
import { formatTime } from "../../utils/tool";
import { storeToRefs } from "pinia";
import { musicStore } from "@/store/music";
import { createWindow, ipcRendererSend } from "../../utils/ipcRenderer";
import { getMusicDetailList, getMusicUrl } from "../../apis/user";

const { songId } = storeToRefs(musicStore());

const playList = ref(Local.get("songList"));
const musicDetail = ref(Local.get("musicInfo"));

const listIndex = ref();

listIndex.value =
  playList.value.map((item) => item.id).indexOf(songId.value) + 1;

watch(
  () => songId.value,
  (val) => {
    songId.value = val;
    nextTick(() => {
      getMusicUrls(val);
      listIndex.value =
        playList.value.map((item) => item.id).indexOf(songId.value) + 1;
    });
  }
);

const getMusicUrls = (id) => {
  getMusicDetailList({ ids: id }).then((res) => {
    musicDetail.value = res.songs[0];
  });
};

const handlePlay = (item) => {
  musicDetail.value = item;
  Local.set("musicInfo", item);
  songId.value = item.id;
  ipcRendererSend("windows-param", {
    emit: "playMusic",
    data: item.id,
    targetName: "home",
  });
};
const handleVideo = (item) => {
  createWindow({
    windowName: "videoDetail",
    path: "videoDetail",
    width: 1360,
    height: 820,
    param: { id: item.mv },
  });
  ipcRendererSend("windows-param", {
    emit: "playVideo",
    targetName: "home",
  });
};
const handleSinger = (id) => {
  createWindow({
    windowName: "singerDetail",
    path: "singerDetail",
    width: 1200,
    height: 820,
    param: { id: id },
  });
};
</script>

<template>
  <div class="play_list">
    <div class="title">正在播放</div>
    <div class="song_list now_music">
      <div v-if="musicDetail" class="item active_position">
        <div class="iconfont icon-bofang num"></div>
        <div class="name">{{ musicDetail.name }}</div>
        <div class="album">
          <span @click="handleSinger( musicDetail.ar[0].id)">{{
            musicDetail.ar[0].name
          }}</span>
        </div>

        <div class="btn">
          <el-button
            @click="handleVideo(musicDetail.mv)"
            size="small"
            v-if="musicDetail.mv != 0"
            type="primary"
            >mv</el-button
          >
        </div>
        <div class="timer">{{ formatTime(musicDetail.dt) }}</div>
      </div>
    </div>
    <div class="title">即将播放</div>

    <div class="song_list song_wrapper">
      <el-scrollbar height="100%">
        <div
          @dblclick="handlePlay(item)"
          :class="{ active_position: item.id === songId }"
          v-for="(item, index) in playList.slice(listIndex)"
          :key="index"
          class="item"
        >
          <div class="num">{{ index + 1 }}</div>
          <div class="name">{{ item.name }}</div>
          <div class="album">
            <span @click="handleSinger(item.ar[0].id)">{{
              item.ar[0].name
            }}</span>
          </div>

          <div class="btn">
            <el-button
              @click="handleVideo(item.mv)"
              size="small"
              v-if="item.mv != 0"
              type="primary"
              >mv</el-button
            >
          </div>
          <div class="timer">{{ formatTime(item.dt) }}</div>
        </div>
        <upbar></upbar>
      </el-scrollbar>
    </div>
  </div>
</template>

<style scoped lang="scss">
.play_list {
  padding: 0px 20px;
  height: 100%;
  .title {
    margin-left: 10px;
    font-size: 35px;
    font-weight: 600;
  }
  .now_music {
    margin-bottom: 30px;
  }
  .song_list {
    margin-top: 20px;
    .item {
      height: 50px;
      justify-content: space-around;
      display: flex;
      align-items: center;
      border-radius: 8px;
      padding: 0 5px;
      &:hover {
        background-color: #f2f2f4;
      }
      .num {
        width: 50px;
        text-align: center;
        font-size: 16px;
        //   font-weight: 600;
      }
      .name {
        width: 40%;
        font-size: 18px;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .album {
        flex: 1;
        font-size: 15px;
        font-weight: 600;
        cursor: pointer;
        span:hover {
          text-decoration: underline;
        }
      }
      .btn {
        font-size: 15px;
        font-weight: 600;
        width: 18%;
        color: #335eea;
        &:hover .el-button {
          transform: scale(1.1);
        }
        .el-button {
          margin-left: 6px;
          background-color: #fff;
          color: rgb(16, 24, 134);
          font-weight: 600;
          border: none;
          border-radius: 5px;
        }
        i {
          &:hover {
            transform: scale(1.1);
          }
        }
      }
      .timer {
        width: 50px;
        text-align: center;
        margin-right: 10px;
        font-size: 18px;
        color: rgb(167, 157, 157);
      }
    }
    .active_position {
      color: #335eea;
      background: #eaeffd;

      .num {
        color: #335eea;
      }
      .album,
      .name,
      .timer {
        color: #335eea;
      }
    }
  }
  .song_wrapper {
    height: calc(100% - 170px);
  }
}
</style>
